#!/usr/bin/python
# -*- coding: utf-8 -*-
# Description: MP3 spider
# Create at 2008-11-16 下午10:14:51
# Author: MK2[fengmk2@gmail.com]
"""Just for get mp3 url and info.
"""

from utilities.web.spider import Spider

class MP3Spider(object):
    """A MP3 spider, getting url and info from web.
    

CREATE TABLE `music` (
  `id` int(11) NOT NULL auto_increment,
  `title` varchar(100) NOT NULL,
  `author` varchar(100) NOT NULL,
  `album` varchar(200),
  `info_url` varchar(1000) NOT NULL,
  PRIMARY KEY  (`id`),
  KEY `title` (`title`),
  KEY `author` (`author`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

CREATE TABLE `music_url` (
  `id` int(11) NOT NULL auto_increment,
  `music_id` int(11) NOT NULL,
  `referer` varchar(1000) NOT NULL,
  `downurl` varchar(1000) NOT NULL,
  `size` varchar(100),
  `speed` int(11),
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
    
    """ 
    
    def __init__(self, start_url, templates):
        self.spider = Spider(start_url, templates)
        
    def run(self):
        for level, url, parenturl, response, \
                content, downurls, softinfo in self.spider.walk():
            self.save_grab_url(url)
            for ftpurl in downurls:
                if not check_ftpip_is_repeat(ftpurl):
                    self.save(ftpurl)
        if self._f is not None:
            self._f.close()